package edu.office.applicationRepository;

import edu.office.entities.*;
import edu.office.tools.StaticFields;
import edu.office.tools.Tool;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Named("teacherAdminRepositoryA")
@ApplicationScoped
public class TeacherAdminRepository extends ApplicationCommonRepository<TeacherAdmin>{

    @Inject
    RoleinfoRepository roleinfoRepository;
    @Inject
     TeacschoolsubjectRepository teacschoolsubjectRepository;
    @Inject
    KnowledgeRepository knowledgeRepository;
    private TeacherAdmin nullTeacherAdmin;

    public TeacherAdmin getNullTeacherAdmin() {
        if (null == nullTeacherAdmin) {
            nullTeacherAdmin = find("select * from teacher_admin where name='TNULL'").firstResult();
        }
        return nullTeacherAdmin;
    }

    public Set<TeacherAdmin> findBy(School school, Subject subject){
        //K-means中K的数量由此可以确定
        List<Teacschoolsubject> teacschoolsubjects= teacschoolsubjectRepository.getTeacschoolsubject(school, subject);
        Set<TeacherAdmin> teacherAdmins=new HashSet<>();
        for(Teacschoolsubject teacschoolsubject: teacschoolsubjects){
            teacherAdmins.add(find("ID",teacschoolsubject.getTEACHERID()).firstResult());
        }
        return  teacherAdmins;
    }

      public TeacherAdmin getSecretary() {
       List<TeacherAdmin> secretaryList =find("select * from teacher_admin where role_id=" + roleinfoRepository.getSecretaryRoleinfo().getID()).list();
       return secretaryList.get(Tool.getRand(secretaryList.toArray()));
    }

    public String myCreate(TeacherAdmin teacherAdmin) {
        String result;
        if (find("NAME", teacherAdmin.getNAME()).list().isEmpty()) {
            teacherAdmin.setPASSWORD(StaticFields.encrypt(teacherAdmin.getPASSWORD()));
            teacherAdmin.setROLE_ID(roleinfoRepository.getTeacherRoleinfo().getID());
            create(teacherAdmin);
            result = "Succeed";
        } else {
            result = teacherAdmin.getNAME() + ": Already Exist";
        }
        return result;
    }

    public List<TeacherAdmin> getTeacherList(String searchName) {
        String conditonString = "";
        if (null != searchName && !searchName.trim().isEmpty()) {
            conditonString = "where  locate ('" + searchName.toLowerCase() + "',LOWER(name))>0";
        }
        return find("select * from Teacher_Admin " + conditonString + " order by name").list();
    }


    public TeacherAdmin getWhom2Examin(User user, Question question, Knowledge knowledge) {
        Teacschoolsubject teacschoolsubject = null;
        Calendar calendar = Calendar.getInstance();
        String currentTime = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DATE);
        if (user instanceof Student)
            if (null != knowledge) {
                teacschoolsubject = teacschoolsubjectRepository.getTeacschoolsubject(null,
                        ((Student)user).getSCHOOL_ID(), knowledge.getSUBJECT_ID(), currentTime);
            } else if (null != question) {
                teacschoolsubject = teacschoolsubjectRepository.getTeacschoolsubject(null,
                        ((Student)user).getSCHOOL_ID(),
                        knowledgeRepository.find("ID", question.getKNOWLEDGE_ID()).firstResult().getSUBJECT_ID(), currentTime);
            } else if (user instanceof TeacherAdmin)
                if (null != knowledge) {
                    teacschoolsubject = teacschoolsubjectRepository.getTeacschoolsubject(null, null,
                            knowledge.getSUBJECT_ID(), currentTime);
                } else if (null != question) {
                    teacschoolsubject = teacschoolsubjectRepository.getTeacschoolsubject(null, null,
                            knowledgeRepository.find("ID", question.getKNOWLEDGE_ID()).firstResult().getSUBJECT_ID(), currentTime);
                } else if (user instanceof Parent)
                    if (null != knowledge) {
                        teacschoolsubject = teacschoolsubjectRepository.getTeacschoolsubject(null,
                                ((Student)user).getSCHOOL_ID(), knowledge.getSUBJECT_ID(), currentTime);
                    } else if (null != question) {
                        teacschoolsubject = teacschoolsubjectRepository.getTeacschoolsubject(null,
                                ((Student)user).getSCHOOL_ID(),
                                knowledgeRepository.find("ID", question.getKNOWLEDGE_ID()).firstResult().getSUBJECT_ID(), currentTime);
                    }
        if (null != teacschoolsubject) {
            return find("ID", teacschoolsubject.getTEACHERID()).firstResult();
        } else {
            return getSecretary();
        }
    }

}
